package com.liuxu.topnews.acitivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.liuxu.topnews.BuildConfig;
import com.liuxu.topnews.service.AccessService;
import com.liuxu.topnews.Adapter.Recycle_Applist_Adapter;
import com.liuxu.topnews.MyApplication;
import com.liuxu.topnews.R;
import com.liuxu.topnews.bean.App;
import com.liuxu.topnews.utils.MultiUtils;
import com.liuxu.topnews.utils.PathUtils;
import com.liuxu.topnews.utils.SettingUtils;
import com.liuxu.topnews.utils.ShareUtil;
import com.liuxu.topnews.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import static android.app.Activity.RESULT_OK;
import static com.liuxu.topnews.MyApplication.globalapps;

public class APP_Fragment extends Fragment {
//
    public boolean check=true;
    public static String version;
    private Fragment[] mFragments;
    public Button button1;
    public Button button2;
    public Button button3;
    public Button button4;
    public Button button5;
    public CheckBox CheckBox1;
    public View view;
    private SwipeRefreshLayout refreshLayout;
    public TextView install;
    public TextView install_count;
    public MainActivity parentActivity;
    public Intent intent;
    public RecyclerView recyclerView;
    public Recycle_Applist_Adapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        switch (BuildConfig.version) {
            case "normal":
                view = inflater.inflate(R.layout.applist_fragment, container, false);
                break;
            case "xm":
                view = inflater.inflate(R.layout.applist_fragment_hw, container, false);
                break;
            case "hw":
                view = inflater.inflate(R.layout.applist_fragment_hw, container, false);
                break;
            default:
                view = inflater.inflate(R.layout.applist_fragment, container, false);
                break;
        }
        parentActivity= (MainActivity ) getActivity();
        initView();
        initListener();

        return view;
    }
    private void initView(){
        button1 = (Button)view.findViewById(R.id.button1);
        button2 = (Button)view. findViewById(R.id.button2);
        button3 = (Button)view. findViewById(R.id.button3);
        button4 = (Button)view. findViewById(R.id.button4);
        button5 = (Button)view. findViewById(R.id.button5);
        install = (TextView) view.findViewById(R.id.install);
        install_count = (TextView) view.findViewById(R.id.install_count);
        refreshLayout = (SwipeRefreshLayout)view.findViewById(R.id.SwipeRefreshLayout);
        refreshLayout.setColorSchemeResources(R.color.orange, R.color.green, R.color.purple,R.color.blue); // 进度动画颜色
        recyclerView= (RecyclerView)view.findViewById(R.id.applist_view);
        install_count.setText(MultiUtils.getInstance().getappcount());
        adapter=new Recycle_Applist_Adapter();
        LinearLayoutManager layoutManager=new LinearLayoutManager(MyApplication.getContext());
        layoutManager.setOrientation((LinearLayoutManager.VERTICAL));
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
        mFragments=new Fragment[]{new APP_Fragment(),new InfoFragment()};
        intent = new Intent(MyApplication.getContext(), AccessService.class);
    }
    private void initListener() {

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SettingUtils.getInstance().saveSetting(globalapps,true);
                adapter.update();
                ToastUtils.getInstance().shownow("保存成功");
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("*/*");//设置类型，我这里是任意类型，任意后缀的可以这样写。
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                startActivityForResult(intent,1);
            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SettingUtils.getInstance().saveSetting(globalapps,false);
                ToastUtils.getInstance().shownow("文件已导出到根目录下：read_set.xml");
            }
        });
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MultiUtils.getInstance().initAppData(true);
                adapter.update();
                ToastUtils.getInstance().shownow("已恢复默认配置");
            }
        });
        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShareUtil.getInstance().celardata(globalapps);
                adapter.update();
                ToastUtils.getInstance().shownow("已清除阅读记录");
            }
        });
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshLayout.setRefreshing(true);
                install_count.setText(MultiUtils.getInstance().getappcount());
                adapter.update();
                refreshLayout.setRefreshing(false);
                ToastUtils.getInstance().shownow("刷新成功");
            }
        });

    }
    public ArrayList<App> getApps(){
        ArrayList<App> apps = new ArrayList<App>();
        RecyclerView recy =view.findViewById(R.id.recycler_view);
        for (int i = 0; i < recy.getChildCount(); i++) {
            App app=new App();
            LinearLayout layout = (LinearLayout) recy.getChildAt(i);
            TextView textView1=layout.findViewById(R.id.appid);
            TextView textView2=layout.findViewById(R.id.appname);
            EditText editText1=layout.findViewById(R.id.word1);
            EditText editText2=layout.findViewById(R.id.word2);
            EditText editText3=layout.findViewById(R.id.word3);
            app.setId(textView1.getText().toString());
            app.setZname(textView2.getText().toString());
            app.setUptime(Integer.parseInt(editText1.getText().toString()));
            app.setDowntime(Integer.parseInt(editText2.getText().toString()));
            app.setPagenumber(Integer.parseInt(editText3.getText().toString()));
            apps.add(app);
        }
        return apps;
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            Uri uri = data.getData();
            String path= new PathUtils().GetPath(uri);
            List<App> apps = SettingUtils.getInstance().GetAppSetXmlByPath(path);
            MultiUtils.getInstance().initAppData(apps);
            adapter.update();
            ToastUtils.getInstance().shownow("导入成功");
        }
    }


}
